﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Commerce;

using Kooboo.Web.Mvc;
using Kooboo.Commerce.Services;


namespace Kooboo.Web.Controllers
{
    public class HomeController : ControllerBase
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            this.BindViewData();

            var serviceClient = new ProductService();

            return View(serviceClient.GetLatestProducts(10).Select(i=>new 
            {
                Id = i.ProductId,
                i.Name,
                Price = i.Price.ToString("C"),
                Image = combianThumbnailImageUrl(i.Thumbnail),
                Url = this.Url.Action<ProductController>(m=>m.View(i.ProductId,null)) 
            }));
        }

        private String combianThumbnailImageUrl(String thumbnailName)
        {
            if (!String.IsNullOrEmpty(thumbnailName))
            {
                return Kooboo.Commerce.Configuration.FindImageUrlBase(Kooboo.Commerce.Configuration.ProductImageFolder, "Thumnail") + thumbnailName;
            }
            else
            {
                return Kooboo.Commerce.Configuration.ImageNotAvailable_Thumbnail;
            }
        }
    }
}
